package blazingbubble.legacy.game;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Vector;

public class BubbleChain
{
	private HashMap<Bubble.Type, Vector<Bubble>> coloredChains = new HashMap<Bubble.Type, Vector<Bubble>>();

	public void add(Bubble bubble, Bubble.Type type)
	{
		addChainIfNotContained(type);
		coloredChains.get(type).add(bubble);
	}

	public boolean contains(Bubble bubble)
	{
        for(Vector<Bubble> bubbles : coloredChains.values())
        {
            if(bubbles.contains(bubble))
            {
                return true;
            }
        }
		return false;
	}

	public boolean isEmpty()
	{
        for(Vector<Bubble> bubbles : coloredChains.values())
        {
            if(!bubbles.isEmpty())
            {
                return false;
            }
        }
		return true;
	}

	public boolean nextToChain(Bubble bubble)
	{
        for(Vector<Bubble> currentSubChain : coloredChains.values())
        {
            for(Bubble currentBubble : currentSubChain)
            {
                if(currentBubble.getAttached().contains(bubble))
                {
                    return true;
                }
            }
        }
		return false;
	}

	private void addChainIfNotContained(Bubble.Type type)
	{
		if(!coloredChains.containsKey(type))
			coloredChains.put(type, new Vector<Bubble>());
	}

	public LinkedList<Bubble> getChainBubblesByCriticalMass(int criticalMass)
	{
		LinkedList<Bubble> criticalMassChainBubbles = new LinkedList<Bubble>();

        for(Vector<Bubble> currentSubChain : coloredChains.values())
        {
            if(currentSubChain.size() < criticalMass)
            {
                continue;
            }
            criticalMassChainBubbles.removeAll(currentSubChain);
            criticalMassChainBubbles.addAll(currentSubChain);
        }

		return criticalMassChainBubbles;
	}
}